package com.aibili.contoller.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aibili.pojo.UserAttention;
import com.aibili.pojo.UserInfo;
import com.aibili.service.ChatService;
import com.aibili.service.UserInfoService;
import com.alibaba.fastjson.JSON;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;
import com.daoshun.exception.CustomException;

@Controller
@RequestMapping("/api/chat")
public class ApiChatContoller extends ApiBaseContoller {
	@Resource
	ChatService chatService;

	@Resource
	private UserInfoService userInfoService;
	
	/**
	 * 添加关注
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/attention", produces = "application/json; charset=UTF-8")
	public String attention(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String from_id = request.getParameter("from_id");
			CommonUtils.validateEmpty(from_id);
			long fid = CommonUtils.parseLong(from_id, 0);
			String to_id = request.getParameter("to_id");
			CommonUtils.validateEmpty(to_id);
			long tid = CommonUtils.parseLong(to_id, 0);
			UserAttention uat = chatService.checkattention(fid, tid);
			if (uat != null) {
				return DsConstant.HAS_ATTENTION;
			}
			UserAttention uatt = new UserAttention();
			Date attention_time = new Date();
			uatt.setFrom_id(fid);
			uatt.setTo_id(tid);
			uatt.setAttention_time(attention_time);
			UserAttention usera = chatService.checkattention(tid, fid);// 检查是否关注
			if (usera != null) {
				uatt.setRelation(2);
				usera.setRelation(2);
				chatService.chage(usera);// 改Relation
			} else {
				uatt.setRelation(1);
			}
			chatService.attention(uatt);
			UserInfo user = userInfoService.getById(fid);
			if (user!=null) {
				List<UserInfo> auser_list = chatService.getAttentionListByUser(user.getId());
				int amount = auser_list.size();
				user.setAttention_num(amount);//关注数
				userInfoService.updateUserInfo(user);
				map.put("user", user);
			}
			UserInfo tuser = userInfoService.getById(tid);
			if (tuser!=null) {
				long count = userInfoService.getfansnum(tid);
				tuser.setFans_num((int)count);
				userInfoService.updateUserInfo(tuser);
				map.put("tuser", tuser);//粉丝数
			}
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return JSON.toJSONString(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 取消关注
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cancel_attention", produces = "application/json; charset=UTF-8")
	public String cancel_attention(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String from_id = request.getParameter("from_id");
			CommonUtils.validateEmpty(from_id);
			long fid = CommonUtils.parseLong(from_id, 0);
			String to_id = request.getParameter("to_id");
			CommonUtils.validateEmpty(to_id);
			long tid = CommonUtils.parseLong(to_id, 0);
			chatService.cancel_attention(fid, tid);
			UserAttention uaAttention = chatService.checkattention(tid, fid);
			if (uaAttention != null) {
				uaAttention.setRelation(1);
				chatService.chage(uaAttention);
			}
			UserInfo user = userInfoService.getById(fid);
			if (user!=null) {
				List<UserInfo> auser_list = chatService.getAttentionListByUser(user.getId());
				int amount = auser_list.size();
				user.setAttention_num(amount);
				userInfoService.updateUserInfo(user);
				map.put("user", user);
			}
			UserInfo tuser = userInfoService.getById(tid);
			if (tuser!=null) {
				long count = userInfoService.getfansnum(tid);
				tuser.setFans_num((int)count);
				userInfoService.updateUserInfo(tuser);
				map.put("tuser", tuser);
			}
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return JSON.toJSONString(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 关注列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/friends", produces = "application/json; charset=UTF-8")
	public String friends(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			CommonUtils.validateEmpty(user_id);
			List<UserInfo> user_list = chatService.getAttentionListByUser(CommonUtils.parseLong(user_id, 0));
			int amount = user_list.size();
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			map.put("user_list", user_list);
			map.put("amount", amount);
			return toJson(map);
		} catch (CustomException e) {
			return e.getMessage();
			
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

}
